﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace belov_test
{
    public partial class MoreInfoForm : Form
    {
        private const int OFFSET = 10;

        private UserInfo m_user;
 
        public MoreInfoForm(UserInfo user)
        {
            InitializeComponent();
            m_user = user;
        }

        private void OnFormLoaded(object sender, EventArgs e)
        {
            m_nameTextBox.Text = m_user.Name;
            m_surnameTextBox.Text = m_user.Surname;
            m_patronymicTextBox.Text = m_user.Patronymic;
            m_lastUseLabel.Text = "Последний вход: " + m_user.LastAccountUsing;
            m_passedTestsLabel.Text = "Количество пройденных тестов: " + m_user.GetTestResultsCount();

            int x = m_passedTestsLabel.Location.X, y = m_passedTestsLabel.Location.Y + m_passedTestsLabel.Height + 5;

            for (int i = 0; i < m_user.GetTestResultsCount(); i++)
            {
                TestResult test = m_user.GetTestResult(i);
                Label label = CreateLabel("Тест №" + (i + 1) + ": ", x, y);
                y = label.Location.Y + label.Height;
                
                String addInfo = "Информация: ";

                addInfo += "время: " + test.Time.ToString() + ", " + 
                               "начало: " + test.BeginTime + ", " + 
                               "окончание: " + test.EndTime;

                Label addLabel = CreateLabel(addInfo, x + OFFSET, y);
                Width = Math.Max(Width, addLabel.Location.X + addLabel.Width + OFFSET);

                y = addLabel.Location.Y + addLabel.Height + OFFSET;
                Width = Math.Max(Width, x + label.Width);

                m_closeBtn.Location = new Point(Width - m_closeBtn.Width - OFFSET, y + label.Height + OFFSET - m_closeBtn.Height);
            }

            m_saveBtn.Location = new Point(m_closeBtn.Location.X - m_saveBtn.Width - m_closeBtn.Width, m_closeBtn.Location.Y);
            ClientSize = new Size(Width, m_closeBtn.Location.Y + m_closeBtn.Height + OFFSET);

            m_saveBtn.Enabled = false;
        }

        private Label CreateLabel(String text, int x, int y)
        {
            Label label = new Label();

            label.Text = text;
            label.AutoSize = true;
            label.Location = new Point(x, y);
            Controls.Add(label);

            return label;
        }

        private void OnCloseBtnClick(object sender, EventArgs e)
        {
            Close();
        }

        private void OnSaveBtnClick(object sender, EventArgs e)
        {
            m_user.Name = m_nameTextBox.Text.Trim();
            m_user.Surname = m_surnameTextBox.Text.Trim();
            m_user.Patronymic = m_patronymicTextBox.Text.Trim();    
            m_saveBtn.Enabled = false;
        }

        public belov_test.UserInfo User
        {
            get { return m_user; }
            set { m_user = value; }
        }

        private void OnTextChanged(object sender, EventArgs e)
        {
            m_saveBtn.Enabled = true;
        }
    }
}
